home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c4 / tvcaptur.cpp < prev    next >
C/C++ Source or Header  |  1993-04-13  |  4KB  |  140 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4. #include <fcntl.h>
  5. #include <sys\stat.h>
  6. #include <iostream.h>
  7.  
  8. #define Uses_otstream
  9. #define Uses_MsgBox
  10. #define Uses_TApplication
  11. #define Uses_TButton
  12. #define Uses_TDeskTop
  13. #define Uses_TEvent
  14. #define Uses_TKeys
  15. #define Uses_TRect
  16. #define Uses_TStatusDef
  17. #define Uses_TStatusItem
  18. #define Uses_TStatusLine
  19. #define Uses_TTerminal
  20. #define Uses_TWindow
  21.  
  22. #include <tv\tv.h>
  23.  
  24. //==============================================================================
  25. class TStreamView:
  26.   public TWindow
  27. {
  28.   int fhSave, fhClone, fhTmp;
  29.   char *fnTmp;
  30.  
  31.   otstream *os;
  32.  
  33.   TTerminal *wn;
  34.  
  35.   public:
  36.     TStreamView( TRect &r, const char *title, int fh, ushort bufSize = 2048 );
  37.     ~TStreamView();
  38.  
  39.     void update();
  40. };
  41.  
  42. //==============================================================================
  43. //==============================================================================
  44. //==============================================================================
  45. TStreamView::TStreamView( TRect &r, const char *title, int fh, ushort bufSize ):
  46.   TWindow( r, title, wnNoNumber ),
  47.   TWindowInit( TStreamView::initFrame )
  48. {
  49.   r = getExtent();
  50.  
  51.   r.grow( -1, -1 );
  52.  
  53.   wn = new TTerminal( r,
  54.                       standardScrollBar( sbHorizontal | sbHandleKeyboard ),
  55.                       standardScrollBar( sbVertical | sbHandleKeyboard ),
  56.                       bufSize );
  57.  
  58.   insert( wn );
  59.  
  60.   os = new otstream( wn );  // this is the stream that will be written to
  61.  
  62.   fhClone = fh;              // clone this handle
  63.   fhSave  = dup( fhClone );  // save original handle
  64.  
  65.   fnTmp = tmpnam( 0 );                                               // create scratch IO file
  66.   fhTmp = open( fnTmp, O_RDWR|O_CREAT|O_BINARY, S_IREAD|S_IWRITE );  // create scratch file
  67.  
  68.   dup2( fhTmp, fhClone );  // attach original to scratch
  69. }
  70.  
  71. //==============================================================================
  72. TStreamView::~TStreamView() {
  73.   dup2( fhSave, fhClone );  // re-attach orignal
  74.   ::close( fhSave );        // close handle
  75.   ::close( fhTmp );         // close scratch file
  76.   ::remove( fnTmp );        // delete scratch file
  77.  
  78.   delete os;
  79. }
  80.  
  81. //==============================================================================
  82. void TStreamView::update() {
  83.   if ( tell( fhTmp ) > 0L ) {  // see if file-pointer moved
  84.     char ch;
  85.     long nBytes = tell( fhTmp );  // count the number of bytes in buffer
  86.  
  87.     lseek( fhTmp, 0L, SEEK_SET );  // reset file-pointer
  88.  
  89.     for ( long n = 0; n < nBytes && ::read( fhTmp, &ch, 1 ) > 0; ++n )  // clean out buffer
  90.       *os << ch;
  91.  
  92.     lseek( fhTmp, 0L, SEEK_SET );  // reset file-pointer again
  93.   }
  94. }
  95.  
  96. //==============================================================================
  97. //==============================================================================
  98. //==============================================================================
  99. class App:
  100.   public TApplication
  101. {
  102.   TStreamView *stdOut;
  103.  
  104.   int count;
  105.  
  106.   public:
  107.     App();
  108.     ~App() { destroy( stdOut ); }
  109.  
  110.     void handleEvent( TEvent &evt ) { TApplication::handleEvent( evt ); }
  111.     void idle();
  112. };
  113.  
  114. //==============================================================================
  115. App::App():
  116.   TProgInit( App::initStatusLine, App::initMenuBar, App::initDeskTop )
  117. {
  118.   stdOut = new TStreamView( TRect( 1, 1, 50, 15 ), "stdout", fileno( stdout ) );
  119.  
  120.   if ( stdOut )
  121.     deskTop->insert( stdOut );
  122.  
  123.   count = 0;
  124. }
  125.  
  126. //==============================================================================
  127. void App::idle() {
  128.   TProgram::idle();
  129.  
  130.   printf( "%d\n", ++count );  // write to stdout!
  131.  
  132.   stdOut->update();
  133. }
  134.  
  135. //==============================================================================
  136. void main() {
  137.   App app;
  138.  
  139.   app.run();
  140. }